
library(dplyr)
library(Matching)

SV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_SV_Stage_2_clean_prefrecoded.csv", header = T, stringsAsFactors = F)
QV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_QV_Stage_2_clean_prefrecoded.csv", header = T, stringsAsFactors = F)




#---- KS Test (with recoded data) ------

sink("California Stage 2 - SV & QV Data/Summaries & Tests/KS Test on QV & SV Referenda CDFs.txt")

cat("KS Test (two-sided) with bootstrapping continuity correction (10k sims) \n \n \n")

cat("Note: '$ks.boot.pvalue' is the bootstrapped p-value. 'P-value' is the uncorrected p-value. \n
    *************************************************************
    ************************************************************* \n")

cat("\n\n********  KS Test (with recoded data) incl. those who abstained  ******** \n")

cat("\n\n\n----------------- Immigration -----------------\n")
ks.boot(SV_data$ImmigrationPref, QV_data$ImmigrationPref, nboots = 10000, alternative = "two.sided")

cat("\n\n\n----------------- Bilingual Educ -----------------\n")
ks.boot(SV_data$EducationPref, QV_data$EducationPref, nboots = 10000, alternative = "two.sided")

cat("\n\n\n----------------- Teacher Tenure -----------------\n")
ks.boot(SV_data$TeachersPref, QV_data$TeachersPref, nboots = 10000, alternative = "two.sided")

cat("\n\n\n----------------- Public Bonds -----------------\n")
ks.boot(SV_data$BondsPref, QV_data$BondsPref, nboots = 10000, alternative = "two.sided")

cat("\n*************************************************************\n")

sink()



#---- Prop Test (with recoded data) with abstained removed ------



sink("California Stage 2 - SV & QV Data/Summaries & Tests/Test of Prop of Abtentions on QV & SV Referenda.txt")

cat("\n\n******** Test of Prop of Abstentions per Referendum ******** \n")

cat("\n\n\n----------------- Immigration -----------------\n")
temp_SV <- SV_data %>% group_by(Ref4_Immigration) %>% summarize(Count = n()) %>% filter(Ref4_Immigration == "Abstain")
temp_QV <- QV_data %>% group_by(Ref4_Immigration) %>% summarize(Count = n()) %>% filter(Ref4_Immigration == "Abstain")

numAbstains  <- c(temp_SV$Count[1], temp_QV$Count[1])
sampleSize <- c(nrow(SV_data), nrow(QV_data))
prop.test(numAbstains, sampleSize)


cat("\n\n\n----------------- Bilingual Educ -----------------\n")
temp_SV <- SV_data %>% group_by(Ref1_BilingualEduc) %>% summarize(Count = n()) %>% filter(Ref1_BilingualEduc == "Abstain")
temp_QV <- QV_data %>% group_by(Ref1_BilingualEduc) %>% summarize(Count = n()) %>% filter(Ref1_BilingualEduc == "Abstain")

numAbstains  <- c(temp_SV$Count[1], temp_QV$Count[1])
sampleSize <- c(nrow(SV_data), nrow(QV_data))
prop.test(numAbstains, sampleSize)


cat("\n\n\n----------------- Teacher Tenure -----------------\n")
temp_SV <- SV_data %>% group_by(Ref2_TeacherTenure) %>% summarize(Count = n()) %>% filter(Ref2_TeacherTenure == "Abstain")
temp_QV <- QV_data %>% group_by(Ref2_TeacherTenure) %>% summarize(Count = n()) %>% filter(Ref2_TeacherTenure == "Abstain")

numAbstains  <- c(temp_SV$Count[1], temp_QV$Count[1])
sampleSize <- c(nrow(SV_data), nrow(QV_data))
prop.test(numAbstains, sampleSize)


cat("\n\n\n----------------- Public Bonds -----------------\n")
temp_SV <- SV_data %>% group_by(Ref3_PublicBonds) %>% summarize(Count = n()) %>% filter(Ref3_PublicBonds == "Abstain")
temp_QV <- QV_data %>% group_by(Ref3_PublicBonds) %>% summarize(Count = n()) %>% filter(Ref3_PublicBonds == "Abstain")

numAbstains  <- c(temp_SV$Count[1], temp_QV$Count[1])
sampleSize <- c(nrow(SV_data), nrow(QV_data))
prop.test(numAbstains, sampleSize)


cat("\n*************************************************************\n")

sink()


